module InputActive

open System

let getchar () =
  let c = Console.Read ()
  if c = -1 then None else Some (char c)

let input =
  Seq.unfold (fun st -> getchar () |> Option.map (fun c -> (c, st))) ()

let newline c =
  if c = '\n' then 1 else 0

let (| Space | NoneSpace |) c =
  match c with
  | ' ' | '\t' | '\n' -> Space
  | _ -> NoneSpace

let wordCount (inWord, wc) c =
  match (inWord, c) with
  | false, NoneSpace -> (true, wc + 1)
  | _, Space -> (false, wc)
  | _ -> (inWord, wc)

let main =
  input
  |> Seq.fold (fun (line, wstate, char) c -> (line + newline c, wordCount wstate c, char + 1)) (0, (false, 0), 0)
  |> (fun (lc, (_, wc), cc) -> (lc, wc, cc))
  |> printf "%A\n"
